package lab8;

import java.util.Arrays;
import java.util.Scanner;

public class HeapImplementation {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int size = 1;
		while (size<=n)
		{
			size*=2;
		}
		size=size*2-1;
		int a[] = new int[size];
		for (int i = 0; i < n; i++)
		{
			a[i+n]=sc.nextInt();
		}
		for (int i = n-1; i >=0; i--)
		{
			if (a[2*i+1]>=a[2*i+2]) a[i]=a[2*i+1];
			else a[i]=a[2*i+2];
		}
		System.out.println(Arrays.toString(a));
	}

}
